﻿using System;
using System.Text.RegularExpressions;
using System.Web;

namespace ShareMarking.Systems.Service
{
    public class HttpHandler : IHttpHandler
    {
        /// <summary>
        /// DTcms的HttpHandler类
        /// </summary>
        #region IHttpHandler Members

        public bool IsReusable
        {
            get { return true; }
        }

        public void ProcessRequest(HttpContext context)
        {
            #region 图片请求压缩处理["jpg", "png", "gif", "jpeg", "bmp"]
            try
            {
                if (!System.IO.File.Exists(context.Server.MapPath(context.Request.Path))) //判断不存在才处理
                {
                    //在此写入您的处理程序实现。
                    string urlpath = context.Request.Path.ToLower();
                    //"jpg", "png", "gif", "jpeg", "bmp"
                    if (urlpath.Contains("jpg"))
                    {
                        //判断是否为需要处理的图片
                        if (Regex.Match(urlpath, @"(.*).jpg_[0-9]*x[0-9]*.jpg").Success)
                        {
                            context.Response.ContentType = "Image/JPEG";
                            //提取文件名
                            string filepath = Regex.Match(urlpath, "(.*).jpg_").Groups[0].Value.Replace(".jpg_", ".jpg");
                            //提取尺寸
                            int towidth = Utils.StrToInt(
                                Regex.Match(urlpath, ".jpg_[0-9]*x[0-9]*.jpg").Groups[0].Value.Replace(".jpg_", "")
                                                                                        .Split('x')[0
                                    ], 0);
                            int toheight = Utils.StrToInt(
                                Regex.Match(urlpath, ".jpg_[0-9]*x[0-9]*.jpg").Groups[0].Value.Replace(".jpg_", "")
                                                                                        .Split('x')[1
                                    ].Replace(".jpg", ""), 0);
                            System.Drawing.Image originalImage =
                                System.Drawing.Image.FromFile(context.Server.MapPath(filepath));

                            int oh, ow, x, y;

                            if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
                            {
                                oh = originalImage.Height;
                                ow = originalImage.Height * towidth / toheight;
                                y = 0;
                                x = (originalImage.Width - ow) / 2;
                            }
                            else
                            {
                                ow = originalImage.Width;
                                oh = originalImage.Width * toheight / towidth;
                                x = 0;
                                y = (originalImage.Height - oh) / 2;
                            }

                            //新建一个bmp图片
                            System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);

                            //新建一个画板
                            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);

                            //设置高质量插值法
                            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;

                            //设置高质量,低速度呈现平滑程度
                            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

                            //清空画布并以透明背景色填充
                            g.Clear(System.Drawing.Color.Transparent);

                            //在指定位置并且按指定大小绘制原图片的指定部分
                            g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight),
                                        new System.Drawing.Rectangle(x, y, ow, oh),
                                        System.Drawing.GraphicsUnit.Pixel);
                            try
                            {
                                //以jpg格式输出缩略图
                                bitmap.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);

                                //保存本地图片
                                bitmap.Save(context.Server.MapPath(context.Request.Path),
                                            System.Drawing.Imaging.ImageFormat.Jpeg);
                            }
                            catch (System.Exception e)
                            {
                                throw e;
                            }
                            finally
                            {
                                originalImage.Dispose();
                                bitmap.Dispose();
                                g.Dispose();
                            }


                        }
                    }
                    else if (urlpath.Contains("jpeg"))
                    {
                        //判断是否为需要处理的图片
                        if (Regex.Match(urlpath, @"(.*).jpeg_[0-9]*x[0-9]*.jpeg").Success)
                        {
                            context.Response.ContentType = "Image/JPEG";
                            //提取文件名
                            string filepath = Regex.Match(urlpath, "(.*).jpeg_").Groups[0].Value.Replace(".jpeg_", ".jpeg");
                            //提取尺寸
                            int towidth = Utils.StrToInt(
                                Regex.Match(urlpath, ".jpeg_[0-9]*x[0-9]*.jpeg").Groups[0].Value.Replace(".jpeg_", "")
                                                                                          .Split('x')
                                    [0], 0);
                            int toheight = Utils.StrToInt(
                                Regex.Match(urlpath, ".jpeg_[0-9]*x[0-9]*.jpeg").Groups[0].Value.Replace(".jpeg_", "")
                                                                                          .Split('x')
                                    [1].Replace(".jpeg", ""), 0);
                            System.Drawing.Image originalImage =
                                System.Drawing.Image.FromFile(context.Server.MapPath(filepath));

                            int oh, ow, x, y;

                            if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
                            {
                                oh = originalImage.Height;
                                ow = originalImage.Height * towidth / toheight;
                                y = 0;
                                x = (originalImage.Width - ow) / 2;
                            }
                            else
                            {
                                ow = originalImage.Width;
                                oh = originalImage.Width * toheight / towidth;
                                x = 0;
                                y = (originalImage.Height - oh) / 2;
                            }

                            //新建一个bmp图片
                            System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);

                            //新建一个画板
                            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);

                            //设置高质量插值法
                            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;

                            //设置高质量,低速度呈现平滑程度
                            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

                            //清空画布并以透明背景色填充
                            g.Clear(System.Drawing.Color.Transparent);

                            //在指定位置并且按指定大小绘制原图片的指定部分
                            g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight),
                                        new System.Drawing.Rectangle(x, y, ow, oh),
                                        System.Drawing.GraphicsUnit.Pixel);
                            try
                            {
                                //以jpg格式输出缩略图
                                bitmap.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);

                                //保存本地图片
                                bitmap.Save(context.Server.MapPath(context.Request.Path),
                                            System.Drawing.Imaging.ImageFormat.Jpeg);
                            }
                            catch (System.Exception e)
                            {
                                throw e;
                            }
                            finally
                            {
                                originalImage.Dispose();
                                bitmap.Dispose();
                                g.Dispose();
                            }


                        }
                    }
                    else if (urlpath.Contains("png"))
                    {
                        //判断是否为需要处理的图片
                        if (Regex.Match(urlpath, @"(.*).png_[0-9]*x[0-9]*.png").Success)
                        {
                            context.Response.ContentType = "Image/Png";
                            //提取文件名
                            string filepath = Regex.Match(urlpath, "(.*).png_").Groups[0].Value.Replace(".png_", ".png");
                            //提取尺寸
                            int towidth = Utils.StrToInt(
                                Regex.Match(urlpath, ".png_[0-9]*x[0-9]*.png").Groups[0].Value.Replace(".png_", "")
                                                                                        .Split('x')[0
                                    ], 0);
                            int toheight = Utils.StrToInt(
                                Regex.Match(urlpath, ".png_[0-9]*x[0-9]*.png").Groups[0].Value.Replace(".png_", "")
                                                                                        .Split('x')[1
                                    ].Replace(".png", ""), 0);
                            System.Drawing.Image originalImage =
                                System.Drawing.Image.FromFile(context.Server.MapPath(filepath));

                            int oh, ow, x, y;

                            if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
                            {
                                oh = originalImage.Height;
                                ow = originalImage.Height * towidth / toheight;
                                y = 0;
                                x = (originalImage.Width - ow) / 2;
                            }
                            else
                            {
                                ow = originalImage.Width;
                                oh = originalImage.Width * toheight / towidth;
                                x = 0;
                                y = (originalImage.Height - oh) / 2;
                            }
                            //新建一个bmp图片
                            System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);

                            //新建一个画板
                            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);

                            //设置高质量插值法
                            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;

                            //设置高质量,低速度呈现平滑程度
                            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

                            //清空画布并以透明背景色填充
                            g.Clear(System.Drawing.Color.Transparent);

                            //在指定位置并且按指定大小绘制原图片的指定部分
                            g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight),
                                        new System.Drawing.Rectangle(x, y, ow, oh),
                                        System.Drawing.GraphicsUnit.Pixel);
                            try
                            {
                                //以jpg格式输出缩略图
                                bitmap.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Png);

                                //保存本地图片
                                bitmap.Save(context.Server.MapPath(context.Request.Path),
                                            System.Drawing.Imaging.ImageFormat.Png);
                            }
                            catch (System.Exception e)
                            {
                                throw e;
                            }
                            finally
                            {
                                originalImage.Dispose();
                                bitmap.Dispose();
                                g.Dispose();
                            }


                        }
                    }
                    else if (urlpath.Contains("gif"))
                    {
                        //判断是否为需要处理的图片
                        if (Regex.Match(urlpath, @"(.*).gif_[0-9]*x[0-9]*.gif").Success)
                        {
                            context.Response.ContentType = "Image/Gif";
                            //提取文件名
                            string filepath = Regex.Match(urlpath, "(.*).gif_").Groups[0].Value.Replace(".gif_", ".gif");
                            //提取尺寸
                            int towidth = Utils.StrToInt(
                                Regex.Match(urlpath, ".gif_[0-9]*x[0-9]*.gif").Groups[0].Value.Replace(".gif_", "")
                                                                                        .Split('x')[0
                                    ], 0);
                            int toheight = Utils.StrToInt(
                                Regex.Match(urlpath, ".gif_[0-9]*x[0-9]*.gif").Groups[0].Value.Replace(".gif_", "")
                                                                                        .Split('x')[1
                                    ].Replace(".gif", ""), 0);
                            System.Drawing.Image originalImage =
                                System.Drawing.Image.FromFile(context.Server.MapPath(filepath));

                            int oh, ow, x, y;

                            if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
                            {
                                oh = originalImage.Height;
                                ow = originalImage.Height * towidth / toheight;
                                y = 0;
                                x = (originalImage.Width - ow) / 2;
                            }
                            else
                            {
                                ow = originalImage.Width;
                                oh = originalImage.Width * toheight / towidth;
                                x = 0;
                                y = (originalImage.Height - oh) / 2;
                            }

                            //新建一个bmp图片
                            System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);

                            //新建一个画板
                            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);

                            //设置高质量插值法
                            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;

                            //设置高质量,低速度呈现平滑程度
                            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

                            //清空画布并以透明背景色填充
                            g.Clear(System.Drawing.Color.Transparent);

                            //在指定位置并且按指定大小绘制原图片的指定部分
                            g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight),
                                        new System.Drawing.Rectangle(x, y, ow, oh),
                                        System.Drawing.GraphicsUnit.Pixel);
                            try
                            {
                                //以jpg格式输出缩略图
                                bitmap.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif);

                                //保存本地图片
                                bitmap.Save(context.Server.MapPath(context.Request.Path),
                                            System.Drawing.Imaging.ImageFormat.Gif);
                            }
                            catch (System.Exception e)
                            {
                                throw e;
                            }
                            finally
                            {
                                originalImage.Dispose();
                                bitmap.Dispose();
                                g.Dispose();
                            }


                        }
                    }
                    else if (urlpath.Contains("bmp"))
                    {
                        //判断是否为需要处理的图片
                        if (Regex.Match(urlpath, @"(.*).bmp_[0-9]*x[0-9]*.bmp").Success)
                        {
                            context.Response.ContentType = "Image/Bmp";
                            //提取文件名
                            string filepath = Regex.Match(urlpath, "(.*).bmp_").Groups[0].Value.Replace(".bmp_", ".bmp");
                            //提取尺寸
                            int towidth = Utils.StrToInt(
                                Regex.Match(urlpath, ".bmp_[0-9]*x[0-9]*.bmp").Groups[0].Value.Replace(".bmp_", "")
                                                                                        .Split('x')[0
                                    ], 0);
                            int toheight = Utils.StrToInt(
                                Regex.Match(urlpath, ".bmp_[0-9]*x[0-9]*.bmp").Groups[0].Value.Replace(".bmp_", "")
                                                                                        .Split('x')[1
                                    ].Replace(".bmp", ""), 0);
                            System.Drawing.Image originalImage =
                                System.Drawing.Image.FromFile(context.Server.MapPath(filepath));

                            int oh, ow, x, y;

                            if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
                            {
                                oh = originalImage.Height;
                                ow = originalImage.Height * towidth / toheight;
                                y = 0;
                                x = (originalImage.Width - ow) / 2;
                            }
                            else
                            {
                                ow = originalImage.Width;
                                oh = originalImage.Width * toheight / towidth;
                                x = 0;
                                y = (originalImage.Height - oh) / 2;
                            }

                            //新建一个bmp图片
                            System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);

                            //新建一个画板
                            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);

                            //设置高质量插值法
                            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;

                            //设置高质量,低速度呈现平滑程度
                            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

                            //清空画布并以透明背景色填充
                            g.Clear(System.Drawing.Color.Transparent);

                            //在指定位置并且按指定大小绘制原图片的指定部分
                            g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight),
                                        new System.Drawing.Rectangle(x, y, ow, oh),
                                        System.Drawing.GraphicsUnit.Pixel);
                            try
                            {
                                //以jpg格式输出缩略图
                                bitmap.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Bmp);

                                //保存本地图片
                                bitmap.Save(context.Server.MapPath(context.Request.Path),
                                            System.Drawing.Imaging.ImageFormat.Bmp);
                            }
                            catch (System.Exception e)
                            {
                                throw e;
                            }
                            finally
                            {
                                originalImage.Dispose();
                                bitmap.Dispose();
                                g.Dispose();
                            }


                        }
                    }
                }
                else
                {
                    System.Drawing.Image originalImage =System.Drawing.Image.FromFile(context.Server.MapPath(context.Request.Path));
                    originalImage.Save(context.Response.OutputStream, originalImage.RawFormat);
                    originalImage.Dispose();
                }
            }
            catch (Exception ex)
            {

            }
            #endregion
        }

        #endregion
    }
}
